//
//  HWMobileGameView.m
//  94hwanGame
//
//  Created by Chendaobin on 2019/8/22.
//  Copyright © 2019年 王治庭. All rights reserved.
//

#import "HWMobileGameView.h"
#import "HWShouYouCollectionViewCell.h"
#import "HWJingPingModel.h"


@interface HWMobileGameView()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
@property(strong,nonatomic) UICollectionView *maincollectionView;

@end

@implementation HWMobileGameView

-(instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor whiteColor];
        //添加一些UI控件
        [self setupUI];
        
        
    }
    
    return  self;
}

// 刷新数据
- (void)roadRefresh {
    
    [self.maincollectionView reloadData];
    
}



// 设置UI
- (void)setupUI {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    CGFloat jianju =  (kScreenW - 78*CFLScaleValue*4 -20) / 3;
    layout.minimumLineSpacing = jianju;
    layout.minimumInteritemSpacing = jianju;
    
    self.maincollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kScreenW,144+12) collectionViewLayout:layout];
    self.maincollectionView.showsHorizontalScrollIndicator = NO;
    self.maincollectionView.delegate = self;
    self.maincollectionView.dataSource = self;
  //  self.maincollectionView.backgroundColor = RGB(217, 247, 250);
    self.maincollectionView.backgroundColor = [UIColor whiteColor];
    //self.maincollectionView.alwaysBounceHorizontal = YES;
    
 
    self.maincollectionView.contentInset = UIEdgeInsetsMake(6, 10, 6, 10);
   // [self.maincollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView"];  //  一定要设置
  //  [self.maincollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cellID"];
    [self.maincollectionView  registerNib:[UINib nibWithNibName:@"HWShouYouCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"cellID"];
    
 //  [UINib nibWithNibName:@"HWShouYouCollectionViewCell" bundle:nil]
    
    self.maincollectionView.userInteractionEnabled = YES;
    [self  addSubview:self.maincollectionView];
    
    
    
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.modelArr.count;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    HWShouYouCollectionViewCell *cell =  [collectionView dequeueReusableCellWithReuseIdentifier:@"cellID" forIndexPath:indexPath];
    cell.index = indexPath;
   
    HWJingPingModel *model = self.modelArr[indexPath.row];
    cell.model = model;
   // cell.backgroundColor =  RGB(217, 247, 250);
    cell.backgroundColor = [UIColor whiteColor];
    return cell;
    
    
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    return CGSizeMake(78*CFLScaleValue,144);
}



- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {

    DLog(@"点击精品手游单元格");
    
    
    HWGameDetailController  *xiangqing = [[HWGameDetailController alloc]init];
    
     HWJingPingModel *model = self.modelArr[indexPath.row];
      xiangqing.gameId = model.id;
      UINavigationController *na =  [self currentNC];
      
    [na pushViewController:xiangqing animated:YES];
    
    
}




- (UINavigationController *)currentNC
{
    if (![[UIApplication sharedApplication].windows.lastObject isKindOfClass:[UIWindow class]]) {
        NSAssert(0, @"未获取到导航控制器");
        return nil;
    }
    UIViewController *rootViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
    return [self getCurrentNCFrom:rootViewController];
}

//递归
- (UINavigationController *)getCurrentNCFrom:(UIViewController *)vc
{
    if ([vc isKindOfClass:[UITabBarController class]]) {
        UINavigationController *nc = ((UITabBarController *)vc).selectedViewController;
        return [self getCurrentNCFrom:nc];
    }
    else if ([vc isKindOfClass:[UINavigationController class]]) {
        if (((UINavigationController *)vc).presentedViewController) {
            return [self getCurrentNCFrom:((UINavigationController *)vc).presentedViewController];
        }
        return [self getCurrentNCFrom:((UINavigationController *)vc).topViewController];
    }
    else if ([vc isKindOfClass:[UIViewController class]]) {
        if (vc.presentedViewController) {
            return [self getCurrentNCFrom:vc.presentedViewController];
        }
        else {
            return vc.navigationController;
        }
    }
    else {
        NSAssert(0, @"未获取到导航控制器");
        return nil;
    }
}





@end
